<template>
<teleport to="#back">
  <div
    class = "d-flex justify-content-center align-items-center h-100 loading-container"
    v-bind:style="{backgroundColor: background || ''}"
  >
    <div class = "loading-content">
      <div class = "spinner-border text-primary" role = "status"></div>
      <p v-if="text" class="text-primary small">{{text}}</p>
    </div>
  </div>
</teleport>
</template>

<script lang = "ts">
import { defineComponent, onUnmounted } from 'vue'

// 对setup函数进行封装，返回options的对象
export default defineComponent({
  // 页面名称
  name: 'Loader',
  // 传递数据
  props: {
    text: {
      type: String
    },
    background: {
      type: String
    }
  },

  // 组合式api入口
  setup () {
    const node = document.createElement('div')
    node.id = 'back'
    document.body.appendChild(node)
    onUnmounted(() => {
      document.body.removeChild(node)
    })
  }
})
</script>

<style lang = "css" scoped>
  .loading-container {
    background: rgba(255, 255, 255, .5);
    z-index: 100;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    text-align: center;
  }
</style>
